const mongoose = require('mongoose')
// 连接mongodb数据库
mongoose.connect('mongodb://127.0.0.1:27017/bilibili')

mongoose.connection.once('open',() => { 
    console.log('连接成功！')

    const bookSchema = new mongoose.Schema({
        name:String,
        author:String,
        price:Number,
        is_hot:Boolean
    })
    let bookModel = mongoose.model('novel',bookSchema)


    // 更新一条
    // bookModel.updateOne({_id:'66cc2969a73ebfe85143e815'},{is_hot:false}).then((res) => { 
    //     console.log('更新成功：',res)
    // }).catch((error) => { 
    //     console.log('更新失败：',error)
    // })

    // 更新多条
    bookModel.updateMany({author:'余华'},{is_hot : true}).then((data) => { 
        console.log('更新成功！',data)
     }).catch((error) => {
        console.log('更新失败',error)
      })
    // mongoose.disconnect()



 })
mongoose.connection.on('close',() => { 
    console.log('关闭连接！')
 })